using System;
using System.Reflection;
using System.Collections.Generic;

using OpenSim.Region.Framework.Scenes;
using OpenSim.Region.Physics.Manager;

using log4net;

namespace Playtime.RegionModule
{
	public class CollisionHandler
	{
		/// <summary>
		/// Reference the logger instance
		/// </summary>
		private static readonly ILog mLog = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

		ScenePresence mPresence;
		
		public CollisionHandler(ScenePresence presence)
		{
			mPresence = presence;
		}

		public void StartListeningForCollisions()
		{
			if(mPresence.PhysicsActor != null)
				mPresence.PhysicsActor.OnCollisionUpdate += OnAgentCollision;
		}
		
		private void OnAgentCollision(EventArgs e)
        {

			if (e == null)
                return;

            CollisionEventUpdate collisionData = (CollisionEventUpdate)e;
			ActorTypes actor = (ActorTypes)collisionData.collidertype;
			mLog.Debug("[" + RegionModule.ModuleName + "]" + mPresence.Firstname + " " + mPresence.Lastname + " collided " + actor);
			
			foreach(KeyValuePair<uint, ContactPoint> o in collisionData.m_objCollisionList)
				Console.WriteLine(o.Value);
			//if ((Math.Abs(Velocity.X) > 0.1e-9f) || (Math.Abs(Velocity.Y) > 0.1e-9f))
            // The Physics Scene will send updates every 500 ms grep: m_physicsActor.SubscribeEvents(
            // as of this comment the interval is set in AddToPhysicalScene
//            if (Animator!=null)
//                Animator.UpdateMovementAnimations();

		}
	}
}

